﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TSL.Model;
using System.Xml.Serialization;
using System.IO;

namespace TSL
{
    class XMLModule
    {
        public void SerializeGesture(string path,string fileName, TSL.Model.Gesture gesture)
        {
            var filenameFormat = fileName + "{0}.xml";
            var savename = string.Format(filenameFormat, "");
            int i = 1;
            while (File.Exists(path + savename))
                savename = string.Format(filenameFormat, "(" + (i++) + ")");
            String savePath = path + savename;
            XmlSerializer mySerializer = new XmlSerializer(typeof(TSL.Model.Gesture));
            StreamWriter myWriter = new StreamWriter(savePath);
            mySerializer.Serialize(myWriter, gesture);
            myWriter.Close();
        }

        public String createName(string path, string name)
        {
            var filenameFormat = name + "{0}.xml";
            var filename = string.Format(filenameFormat, "");
            int i = 2;
            while (File.Exists(path+filename))
                filename = string.Format(filenameFormat, "(" + (i++) + ")");

            return filename;
        }

        public TSL.Model.Gesture DeserializeGesture(String path)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(TSL.Model.Gesture));
            TextReader textReader = new StreamReader(path);
            TSL.Model.Gesture g = (TSL.Model.Gesture)deserializer.Deserialize(textReader);
            textReader.Close();
            return g;
        }
    }
}
